package com.hightop.axure.automic;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.FieldDefaults;

import java.nio.file.Paths;
import java.util.Optional;
import java.util.function.Supplier;

/**
 * 环境变量
 * @author xiehai
 * @date 2022/08/29 18:23
 */
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
@RequiredArgsConstructor
enum Environments {
    /**
     * rp应用路径
     */
    AXURE("axure", "C:\\Program Files (x86)\\Axure\\Axure RP 9\\AxureRP9.exe"),
    /**
     * 默认浏览器
     */
    BROWSER("browser", "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"),
    /**
     * WinAppDriver url
     */
    DRIVER_URL("driverUrl", "http://localhost:4723"),
    /**
     * axure工作空间
     */
    WORKSPACE("workspace", () -> {throw new RuntimeException("axure workspace必须设置");}) {
        @Override
        String getValue() {
            return Paths.get(super.getValue()).toString();
        }
    },
    /**
     * 文件名
     */
    RP_FILE_NAME("rpFileName", () -> {throw new RuntimeException("rp文件名必须设置");});

    /**
     * 属性key
     */
    @Getter
    String key;
    /**
     * 属性默认值
     */
    Supplier<String> defaultValue;

    Environments(String key, String defaultValue) {
        this(key, () -> defaultValue);
    }

    /**
     * 获取环境变量值
     * @return 字符串值
     */
    String getValue() {
        return Optional.ofNullable(System.getProperty(this.getKey())).orElseGet(this.defaultValue);
    }
}
